<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<script type="text/javascript" src="../../lib/commonUtil.js"></script>
<script type="text/javascript" src="../../lib/user-authorization.js"></script>
<script type="text/javascript" src="../../lib/role-authorization.js"></script>
<title>授权管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
	Ext.QuickTips.init();
    Ext.define('roleModel', {
        extend: 'Ext.data.Model',
        fields: [
            'role_id','role_name','role_key','role_desc','role_enable'
        ],
        idProperty: 'role_id'
    });
    
    var store_roles = Ext.create('Ext.data.Store', {
        pageSize: 20,
        model: 'roleModel',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/role/list',
            reader: {
               type: 'json',
               root: 'data',
               totalProperty: 'count'
            },
            simpleSortMode: true,
			listeners:{
                'exception':function( theproxy, response, operation, options ){
                	var data = eval("("+response.responseText+")");
                	if(!data.success){
                		Ext.Msg.alert('信息提示',data.msg);
                	} else {
                		Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                	}
                }
            }
        },
        sorters: [
            {
                property: 'role_id'
            }
        ]
    });
    
    var grid_roles = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store_roles,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: 'role_id',
                dataIndex: 'role_id',
                hidden: true
            },
            {
                text: '角色名称',
                dataIndex: 'role_name',
                width : 180
            },
            {
                text: '角色编码',
                dataIndex: 'role_key',
                width : 180
            },{
            	text:'角色描述',
            	dataIndex:'role_desc',
            	width:400
            },{
            	text:'状态',
            	dataIndex:'role_enable',
            	width:100,
            	renderer:function(value){
            		if(value =='0' || value == '' || value == null){
            			return "<font color='green'>有效</font>";
            		} else {
            			return "<font color='red'>禁用</font>"
            		}
            	}
            }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store_roles,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
			afterPageText: "共 {0} 页",
			firstText:"首页",
			lastText:"末页",
			nextText:"下一页",
			prevText:"上一页",
			refreshText:"刷新"
        }),
        tbar: [
			{
			    pressed: true,
			    icon : '../../img/add.png',
			    text: "创建角色",
			    handler: function(){add_role()}
			},{
                pressed: true,
                text: "修  改",
                icon : '../../img/edit.png',
                handler: function(){upd_role()}
            },{
                pressed: true,
                icon : '../../img/delete.png',
                text: "删  除",
                handler: function(){del_role()}
            },{
            	text: "加入用户",
            	icon: '../../img/user.png',
            	handler:function(){
            		var records = grid_roles.getSelectionModel().getSelection();
                	if(records.length > 1){
            			Ext.Msg.alert("信息提示","只能选择一条角色加入用户！");
            			return;
            		} 
                	if(records.length == 0){
                		Ext.Msg.alert("信息提示","请选择需要加入用户的角色！");
            			return;
                	}
            		join_user(records[0].get('role_id'),records[0].get('role_name'));
            	}
            },{
            	text: "角色授权",
            	icon: '../../img/cog_edit.png',
            	handler: function(){
            		var records = grid_roles.getSelectionModel().getSelection();
                	if(records.length > 1){
            			Ext.Msg.alert("信息提示","只能选择一条角色授权！");
            			return;
            		} 
                	if(records.length == 0){
                		Ext.Msg.alert("信息提示","请选择需要授权的角色！");
            			return;
                	}
                	if(records[0].get('role_enable') == 1 || records[0].get('role_enable') == '1'){
                		Ext.Msg.alert("信息提示","禁用角色无法授权！");
            			return;
                	}
                	var roleId = records[0].get('role_id');
                	authorization(roleId);
            	}
            }
        ]
    });
    store_roles.loadPage(1);
	
	var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',//框式布局
        items: grid_roles
    });
	
	var addWin;
    var addForm = new Ext.FormPanel({
    	url:'/role/edit',
    	method:'POST',
    	frame:true,
    	bodyBorder:false,
    	bodyPadding:'20 20 0',
    	layout:'form',
        plugins: {
            ptype: 'datatip'
        },
    	fieldDefaults: {
    		msgTarget: 'side',
            labelWidth: 120
        },
    	items:[{
    		xtype:'hidden',
    		name:'role_id',
    		id:'roleid',
    		value:''
    	},{
    		xtype:'textfield',
    		fieldLabel:'角色名称',
    		name:'role_name',
    		maxLength:50,
    		allowBlank:false,
    		tooltip:'请输入角色名称',
    		id:'role_name'
    	},{
    		xtype:'textfield',
    		fieldLabel:'角色唯一编码',
    		tooltip:'请输入角色编码',
    		allowBlank:false,
    		maxLength:50,
    		name:'role_key',
    		id:'role_key',
       		validateOnChange:false, 
       		validator:function(){
       			var _value = this.getValue();
       			var _flag = true;
   				if(!Ext.isEmpty(_value) && Ext.isEmpty(Ext.getCmp('roleid').getValue())){
   					Ext.Ajax.request({
   						async:false,
   	    	            url: '/role/check?role_key='+_value,
   	    	            method : 'GET',
   	    	         	scope : this,// 如果通过 ajax 访问后台数据记得要加 async:false 同步
   	    	            success: function(response, opts) {
   	    	                var data = eval("("+response.responseText+")");
   	    	             	_flag = data.success;
   	    	            },
   	    	            failure: function(response, opts) {
   	    	            	var data = eval("("+response.responseText+")");
   	    	            	if(!data.success){
   	    	            		Ext.Msg.alert('信息提示',data.msg);
   	    	            	} else {
   	    	            		Ext.Msg.alert("信息提示","系统忙，请稍候再试");
   	    	            	}
   	    	            }
   	    	        });
   				}
   				if (_flag) {
   					return true;
   				} else {
   					this.markInvalid("该角色编码已存在！");
   					return false;
   				}
       		}
    	},{
            xtype: 'radiogroup',
            fieldLabel: '资源状态',
            vertical: true,
            id:'radio_enable',
            //layout: 'anchor',
            items: [
				{
					checked:true,
				    boxLabel: '有效',
				    name: 'role_enable',
				    id:'enable0',
				    inputValue: '0'
				}, {
				    boxLabel: '禁用',
				    name: 'role_enable',
				    id:'enable1',
				    inputValue: '1'
				}
            ]
        },{
        	xtype:'textarea',
        	fieldLabel:'角色描述',
        	allowBlank:true,
        	name:'role_desc',
        	id:'role_desc',
        	maxLength:200 
        }],
    	buttons:[{ 
    		text:'保  存',
    		icon:'../../img/accept.png',
    		handler:function(){
    			if(!addForm.getForm().isValid()){
    				 Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
    				 return;
                }
    			if(Ext.getCmp('role_name').getValue().replace(/\s+/g,"") == ''){
       				Ext.Msg.alert('信息提示','角色名称不能为空！');
      				return;
       			}
    			if(Ext.getCmp('role_key').getValue().replace(/\s+/g,"") == ''){
       				Ext.Msg.alert('信息提示','角色编码不能为空！');
      				return;
       			}
            	var formValues = addForm.form.getValues();
                addForm.form.submit({
                	waitMsg:'正在保存数据...',
                	success:function(form,action){
            			if(action.result.success){
            				Ext.Msg.alert('信息提示',action.result.msg);
                			store_roles.reload();
	                		addForm.form.reset();
	            			addWin.hide();
                		} else {
                			Ext.Msg.alert('信息提示',Ext.encode(action.result.msg));
                		}
                	},
                	failure:function(form,action){
                		if(!action.result.success){
                			Ext.Msg.alert('信息提示',Ext.encode(action.result.msg));
                		} else {
                			Ext.Msg.alert('信息提示','操作失败，请重试！');
                		}
                	}
                });
    		}
    	},{
    		text:'关  闭',
    		icon:'../../img/close.png',
    		handler:function(){
    			this.up('form').getForm().reset();
    			addWin.hide();
    		}
    	}]
    });
    function add_role(){
    	if(!addWin){
    		addForm.form.reset();
    	}
    	addWin = Ext.create('Ext.Window', {
    		title: '添加角色信息',
    		width: 500,
    		height: 300,
    		bodyPadding: '0 0 0',
    		'z-index': 9999999,
    		constrainHeader: true,
    		closeAction:'hide',
    		layout: 'fit',
    		items: addForm
    	});
    	addWin.show();
    }
    function upd_role(){
    	var records = grid_roles.getSelectionModel().getSelection();
    	if(records.length > 1){
			Ext.Msg.alert("信息提示","只能选择一条数据进行修改！");
			return;
		} 
    	if(records.length == 0){
    		Ext.Msg.alert("信息提示","请选择需要修改的数据！");
			return;
    	}
    	if(!addWin){
	    	addWin = Ext.create('Ext.Window', {
	    		title: '修改角色信息',
	    		width: 500,
	    		height: 340,
	    		bodyPadding: '0 0 0',
	    		'z-index': 9999999,
	    		constrainHeader: true,
	    		closeAction:'hide',
	    		layout: 'fit',
	    		items: addForm
	    	});
    	}
    	addForm.form.loadRecord(records[0]);
    	var radio_enable;
    	if(records[0].get('role_enable') == 0 || records[0].get('role_enable') == '' || records[0].get('role_enable') == null)
    		radio_enable = Ext.getCmp("radio_enable").getComponent('enable0');
    	if(records[0].get('role_enable') == 1)
    		radio_enable = Ext.getCmp("radio_enable").getComponent('enable1');
    	radio_enable.setValue(true);
    	addWin.show();
    }
    
    function del_role(){
    	var records = grid_roles.getSelectionModel().getSelection();
    	if(records.length == 0){
    		Ext.Msg.alert("信息提示","请选择需要删除的数据！");
			return;
    	}
    	Ext.MessageBox.confirm('信息提示','确认删除吗？',function(val){
    		if(val == 'yes'){
    	    	Ext.Ajax.request({
    	            url: '/role/del/'+records[0].get('role_id'),
    	            method : 'POST',
    	            success: function(response, opts) {
    	                var data = eval("("+response.responseText+")");
    	                if(data.success){
                            Ext.Msg.alert('信息提示',data.msg,function(){
                                store_roles.reload();
                            });
                        } else {
                            Ext.Msg.alert('信息提示',data.msg);
                        }
    	            },
    	            failure: function(response, opts) {
    	            	var data = eval("("+response.responseText+")");
    	            	if(!data.success){
    	            		Ext.Msg.alert('信息提示',Ext.encode(data.msg));
    	            	} else {
    	            		Ext.Msg.alert("信息提示","系统忙，请稍候再试");
    	            	}
    	            }
    	        });
    		}
    	});
    }
    
});
</script>
<body>

</body>
</html>